package com.loooooo.pm2024.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.util.*;

public class JsonUtils {

    static JsonMapper jsonMapper = new JsonMapper();
    static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//.Inclusion.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        objectMapper.setLocale(Locale.CHINA);
    }

    public static <T> T deserializeFromString(String text, Class<T> clazz) {
        try {
            return objectMapper.readValue(text, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T deserialize(T obj, Class<T> clazz) {
        return deserializeFromString(serializeToString(obj), clazz);
    }

//    public static <T> List<T> deserializeListFromString(String text, Class<T> clazz) {
//        try {
//            return objectMapper.readerForListOf(clazz).readValue(text);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public static JsonNode parseNode(String text) {
        try {
            return objectMapper.readTree(text);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T deserializeFromString(String text, TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(text, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String serializeToString(Object obj) {
        try {
            return jsonMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Map<String, Object> data = new HashMap<>();
        List<String> ls = new ArrayList<>();
        ls.add("aaa");
        ls.add("bbb");
        data.put("list", ls);

        Map<String, Object> d2 = new HashMap<>();
        d2.put("name", "AAA");
        Map<String, Object> d3 = new HashMap<>();
        d3.put("name", "d3");
        d2.put("d3", d3);
        data.put("d2", d2);
        String text = serializeToString(data);
        System.out.println(text);
        JsonNode obj = deserializeFromString(text, JsonNode.class);
        System.out.println(obj);

    }


}
